package com.jinzhi.jzweb.controller;


import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.jinzhi.common.annotation.Log;
import com.jinzhi.common.base.BaseController;
import com.jinzhi.common.type.EnumErrorCode;
import com.jinzhi.common.utils.EhCacheUtil;
import com.jinzhi.common.utils.Result;
import com.jinzhi.common.utils.StringUtil;
import com.jinzhi.jzweb.StuSignUpComVos;
import com.jinzhi.jzweb.domain.ExaminationBatchComDO;
import com.jinzhi.jzweb.domain.MarkDO;
import com.jinzhi.jzweb.domain.StuSignUpComDO;
import com.jinzhi.jzweb.domain.StuSignUpRequest;
import com.jinzhi.jzweb.pojo.ErroeDownVo;
import com.jinzhi.jzweb.pojo.ErrorCodeVo;
import com.jinzhi.jzweb.pojo.StuSignUpComRecordVo;
import com.jinzhi.jzweb.pojo.StuSignUpComVo;
import com.jinzhi.jzweb.service.ExaminationAnswerSheetComService;
import com.jinzhi.jzweb.service.ExaminationBatchComService;
import com.jinzhi.jzweb.service.MarkService;
import com.jinzhi.jzweb.service.StuSignUpComService;
import com.jinzhi.jzweb.utils.EasyPoiUtil;
import com.jinzhi.sys.domain.UserDO;
import com.jinzhi.sys.service.UserService;
import io.swagger.annotations.*;
import org.apache.commons.lang.StringUtils;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

/**
 * 
 * <pre>
 * 考生报名   检录
 * </pre>
 * <small> 2023-05-05 08:52:02 | lance</small>
 */
@Controller
@RequestMapping("/jzweb/stuSignUpComRecord")
@Api(value = "检录",tags = "检录")
public class StuSignUpComRecordController extends BaseController {
	@Autowired
	private StuSignUpComService stuSignUpComService;

	@Autowired
	private StuSignUpComService stuSignUpService;

	@Autowired
	private ExaminationBatchComService examinationBatchService;


	@ApiOperation(value = "检录页面入口", notes = "检录页面入口")
	@ApiResponses({@ApiResponse(code = 0, message = "操作成功"),
			@ApiResponse(code = 500, message = "服务器内部异常")})
	@GetMapping()
	@RequiresPermissions("jzweb:stuSignUpCom:stuSignUpComRecord")
	String StuSignUpCom(Model model){
		model.addAttribute("types",3);
	    return "jzweb/stuSignUpCom/stuSignUpComRecord";
	}

	/**
	 * 录入检录号
	 * @param id
	 * @param model
	 * @return
	 */
	@GetMapping("/stuSignUpComRecord/{id}")
	@ApiOperation(value = "录入检录号", notes = "录入检录号")
	@ApiResponses({@ApiResponse(code = 0, message = "操作成功"),
			@ApiResponse(code = 500, message = "服务器内部异常")})
	@RequiresPermissions("jzweb:stuSignUpCom:updateRecord")
	String stuSignUpComEnter(@PathVariable("id") Long id,Model model){
		StuSignUpComDO stuSignUpComDO=	stuSignUpService.selectById(id);
		model.addAttribute("stuSignUpCom",stuSignUpComDO);
		return "jzweb/stuSignUpCom/enterRecord";
	}

	/**
	 * 保存录入检录号
	 * @param stuSignUpCom
	 * @return
	 */
	@ApiOperation(value = "保存录入检录号", notes = "保存录入检录号")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "StuSignUpComDO", value = "StuSignUpComDO", required = true, dataType = "StuSignUpComDO"),
	})
	@ApiResponses({@ApiResponse(code = 0, message = "操作成功"),
			@ApiResponse(code = 500, message = "服务器内部异常")})
	@RequestMapping(value = "/updateRecord",method = RequestMethod.POST)
	@ResponseBody
	public Result<String>  updateEnter(@RequestBody StuSignUpComDO stuSignUpCom){
		if(stuSignUpCom.getRecordNumber()==null){
			return  Result.fail("请输入检录号!");
		}
		StuSignUpComDO stuSignUpComDO1=	stuSignUpComService.selectById(stuSignUpCom.getId());
		//检录号不能重复
		List<StuSignUpComDO> signs = stuSignUpComService.selectList(new EntityWrapper<StuSignUpComDO>().
				eq("record_number",stuSignUpCom.getRecordNumber()).
				eq("cbatch_id",stuSignUpComDO1.getCbatchId()));
		if(!signs.isEmpty()){
			for (StuSignUpComDO sign : signs) {
				if(!stuSignUpCom.getId().equals(sign.getId())){
					return Result.fail("检录号不能重复");
				}
			}
		}
		boolean update = stuSignUpComService.updateById(stuSignUpCom);
		return update ? Result.ok() : Result.fail();
	}



	/**
     * 选手检录信息导出
     *
     * @param response
     */
	@ApiOperation(value = "选手检录信息导出", notes = "选手检录信息导出")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "StuSignUpComDO", value = "StuSignUpComDO", required = true, dataType = "StuSignUpComDO"),
	})
	@ApiResponses({@ApiResponse(code = 0, message = "操作成功"),
			@ApiResponse(code = 500, message = "服务器内部异常")})
	@GetMapping("/downloadImportRecord")
	@RequiresPermissions("jzweb:stuSignUpCom:importRecord")
	@ResponseBody
	public Result<String> downloadImportTemplate(HttpServletResponse response,StuSignUpComDO stuSignUpComDTO) {
		Result result = new Result();
		try {
			List<StuSignUpComRecordVo> list1 = new LinkedList<>();
			Wrapper<StuSignUpComDO> wrapper = new EntityWrapper<StuSignUpComDO>().
					orderBy("id", false);
			if(StringUtil.notEmpty(stuSignUpComDTO.getName())){
				wrapper.like("name",stuSignUpComDTO.getName());
			}
			if(StringUtils.isNotBlank(stuSignUpComDTO.getCbatchName())){
				wrapper.eq("cbatch_id",stuSignUpComDTO.getCbatchName());
			}

			if(StringUtil.notEmpty(stuSignUpComDTO.getIdNumber())){
				wrapper.like("id_number", stuSignUpComDTO.getIdNumber());
			}
			List<StuSignUpComDO> list=	stuSignUpComService.selectList(wrapper);
			if(!list.isEmpty()){
				list = list.stream()
						.sorted(Comparator.comparing(StuSignUpComDO::getComNums))
						.collect(Collectors.toList());
			}
			for(StuSignUpComDO li:list){
				StuSignUpComRecordVo stuSignUpComVo=new StuSignUpComRecordVo();
				stuSignUpComVo.setRecordNumber("-");
				if (StringUtil.isNotNull(li.getRecordNumber())) {
					stuSignUpComVo.setRecordNumber(li.getRecordNumber());
				}
				stuSignUpComVo.setComNums(li.getComNums());
				stuSignUpComVo.setName(li.getName());
				stuSignUpComVo.setIdNumber(li.getIdNumber());
				stuSignUpComVo.setTicketNumber(li.getTicketNumber());
				list1.add(stuSignUpComVo);
			}

			//导出操作
			EasyPoiUtil.exportExcel(list1, "选手检录信息导出", "选手检录信息导出", StuSignUpComRecordVo.class, "选手检录信息导出", response);
		}catch (Exception e){
			e.printStackTrace();
			return result.fail();
		}
		return result.ok();
	}

}
